package com.edu.common.tags;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.lang.StringUtils;

public class ResourceAccessTag extends BodyTagSupport{

	private static final long serialVersionUID = 1L;
	
	private String resourceUrl;
	
	public static List<String> isAllowUrls = new ArrayList<String>();
	
	static{
		isAllowUrls.add("/html/welcome.shtml");
	}
	
	@Override
	public void doInitBody() throws JspException {
		super.doInitBody();
	}
	
	@Override
	public int doEndTag() throws JspException {
		String targetUrl = getResourceUrl();
		if(StringUtils.isNotBlank(targetUrl)){
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			targetUrl = StringUtils.removeStart(targetUrl, request.getContextPath());
			if(isAllowUrls.contains(targetUrl)){
				try {
					String bodyContentStr = getBodyContent().getString();
					if (StringUtils.isNotBlank(bodyContentStr)) {
						JspWriter out = getBodyContent().getEnclosingWriter();
						out.println(bodyContentStr);
					}
				} catch (IOException e) {
					e.printStackTrace();
				} 
			}
		}
		return EVAL_PAGE;
	}

	public String getResourceUrl() {
		return resourceUrl;
	}

	public void setResourceUrl(String pResourceUrl) {
		resourceUrl = pResourceUrl;
	}
	
	

}
